﻿using log4net;
using MXComponent.DAL;
using MXComponent.Views.material;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MXComponent.Views.materialwarehouse
{
    /// <summary>
    /// materialwarehouseList.xaml 的交互逻辑
    /// </summary>
    public partial class materialwarehouseList : Page
    {
        public static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private materialwarehouseDB mwdb = new materialwarehouseDB();

        public materialwarehouseList()
        {
            InitializeComponent();
        }

        private void BtnSearch_Click(object sender, RoutedEventArgs e)
        {
            BindGrid();
        }

        private void BindGrid()
        {
            try
            {
                StringBuilder strsql = new StringBuilder(" H.active=1 ");
                if (!string.IsNullOrWhiteSpace(this.txtwarehousename.Text))
                {
                    strsql.AppendFormat(" and H.warehousename like '%{0}%'", this.txtwarehousename.Text.Trim());
                }
                if (!string.IsNullOrWhiteSpace(this.txtMaterialname.Text))
                {
                    strsql.AppendFormat(" and M.materialname like '%{0}%'", this.txtMaterialname.Text.Trim());
                }
                DataSet ds = mwdb.GetList(strsql.ToString());
                this.dgvList.ItemsSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                MessageBox.Show(ex.Message);
            }
        }

        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            materialwarehousePage mwp = new materialwarehousePage();
            bool? result = mwp.ShowDialog();
            if (result == true)
            {
                BindGrid();
            }
        }

        private void BtnUpdate_Click(object sender, RoutedEventArgs e)
        {
            if (dgvList.SelectedItem == null)
            {
                MessageBox.Show("请先选择一行数据!");
                return;
            }
            try
            {
                DataRowView DRV = (DataRowView)dgvList.SelectedItem;
                int warehouseid = int.Parse(DRV.Row["warehouseid"].ToString());//获取选中行的name列内容.SelectedItem;
                materialwarehousePage mwp = new materialwarehousePage(warehouseid);
                bool? result = mwp.ShowDialog();
                if (result == true)
                {
                    BindGrid();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                MessageBox.Show(ex.Message);
            }
        }

        private void BtnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (dgvList.SelectedItem == null)
            {
                MessageBox.Show("请先选择一行数据!");
                return;
            }
            MessageBoxResult dr = MessageBox.Show("请确认是否删除？", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
            if (dr == MessageBoxResult.Cancel)
            {
                return;
            }
            try
            {
                DataRowView DRV = (DataRowView)dgvList.SelectedItem;
                int warehouseid = int.Parse(DRV.Row["warehouseid"].ToString());//获取选中行的name列内容.SelectedItem;
                mwdb.DeleteRecord(warehouseid);
                MessageBox.Show("删除成功!");
                BindGrid();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                MessageBox.Show(ex.Message);
            }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            this.dgvList.LoadingRow += new EventHandler<DataGridRowEventArgs>(this.dataGridEquipment_LoadingRow);
            BindGrid();
        }

        private void dataGridEquipment_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

        private void dgvList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DataGrid datagrid = sender as DataGrid;
            Point aP = e.GetPosition(datagrid);
            IInputElement obj = datagrid.InputHitTest(aP);
            DependencyObject target = obj as DependencyObject;


            while (target != null)
            {
                if (target is DataGridRow)
                {
                    DataRowView DRV = (DataRowView)dgvList.SelectedItem;
                    int warehouseid = int.Parse(DRV.Row["warehouseid"].ToString());//获取选中行的name列内容.SelectedItem;
                    materialwarehousePage mwp = new materialwarehousePage(warehouseid);
                    bool? result = mwp.ShowDialog();
                    if (result == true)
                    {
                        BindGrid();
                    }
                }
                target = VisualTreeHelper.GetParent(target);
            }

        }
    }
}